/*
 * Copyright (c) Huoty, All rights reserved
 * Author: Huoty <sudohuoty@163.com>
 * CreateTime: 2020-11-02 15:13:18
 *
 */

#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>        /* For mode constants */
#include <fcntl.h>           /* For O_* constants */
#include <errno.h>

#define MMAP_DATA_SIZE 1024

int main(void)
{
        char *data;
        char *name;
        int fd = shm_open(name, O_CREAT|O_RDWR, 0777);
        printf("open %s fd: %d", name, fd);
        if (fd < 0) {
            printf("shm_open failed!\n");
            return -1;
        }

        ftruncate(fd, MMAP_DATA_SIZE);

        char buf[1024];
        int len = sprintf(buf,"This is a share memory by write! ! %d\n",fd);
        if (write(fd, buf, len) <= 0) {
            printf("write file %d failed!%d\n",len,errno);
        }

        close(fd);
        // getchar();

        shm_unlink(name);

        return 0;
}
